package com.gongpingjialoan.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.adapter.car.SortAdapter;
import com.gongpingjialoan.bean.car.SortBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/20.
 */
public class PricePopupWindow extends PopupWindow implements AdapterView.OnItemClickListener {


    private Context mContext;

    private View mMenuView;

    private ListView listView;

    private List<SortBean> sortBeans;

    private SortAdapter sortAdapter;

    private Button sureButton;

    private ISelctedPrice mISelctedPrice;

    private String min, max;

    private EditText min_price_et;

    private EditText max_price_et;
    private int maxPrice;


    public void setISelctedPrice(ISelctedPrice ISelctedPrice) {
        mISelctedPrice = ISelctedPrice;
    }

    public PricePopupWindow(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.sort_popup, null);

        listView = (ListView) mMenuView.findViewById(R.id.listview);
        View priceFootView = inflater.inflate(R.layout.price_foot_view, null);
        listView.addFooterView(priceFootView);
        mMenuView.findViewById(R.id.price_popup).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        min_price_et = (EditText)priceFootView.findViewById(R.id.min_price_et);
        max_price_et = (EditText)priceFootView.findViewById(R.id.max_price_et);

        sureButton = (Button) priceFootView.findViewById(R.id.sure_button);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sortAdapter.setPosition(-1);
                min = min_price_et.getText().toString();
                max= max_price_et.getText().toString();
                if(TextUtils.isEmpty(min) || TextUtils.isEmpty(max)){
                    Toast.makeText(mContext,"请输入价格",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(Float.parseFloat(max) > maxPrice){
                    Toast.makeText(mContext,"您输入的最大价格过高，请重新输入",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(Float.parseFloat(min) > Float.parseFloat(max)){
                    Toast.makeText(mContext,"您输入的最小价格大于最大价格，请重新输入",Toast.LENGTH_SHORT).show();
                    return;
                }
                mISelctedPrice.select(min + "-" + max);
                dismiss();
            }
        });

        // mMaterialRangeSlider.setMinMax(min ,max);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimBottom);
        listView.setOnItemClickListener(this);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
        initData();
        sortAdapter = new SortAdapter(sortBeans, mContext);
        listView.setAdapter(sortAdapter);
        sortAdapter.setPosition(-1);
    }


    private void initData() {
        SortBean sortBean;
        sortBeans = new ArrayList<SortBean>();
        sortBean = new SortBean("不限价格", "");
        sortBeans.add(sortBean);
        sortBean = new SortBean("3万以内", "0-3");
        sortBeans.add(sortBean);
        sortBean = new SortBean("3-5万", "3-5");
        sortBeans.add(sortBean);
        sortBean = new SortBean("5-10万", "5-10");
        sortBeans.add(sortBean);
        sortBean = new SortBean("10-15万", "10-15");
        sortBeans.add(sortBean);
    }


    public void clearPrice() {
        min_price_et.setText("");
        max_price_et.setText("");
        sortAdapter.setPosition(-1);
    }

    public void setMax(int price) {
        min_price_et.setText("");
        max_price_et.setText("");
        maxPrice = price;
        sortAdapter.setPosition(-1);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        min_price_et.setText("");
        max_price_et.setText("");
        sortAdapter.setPosition(position);
        mISelctedPrice.select(sortBeans.get(position).getValue());
        dismiss();
    }



    public interface ISelctedPrice {
        public void select(String price);
    }
}
